using System.Collections.Generic;
using FrameWork;
using System;

namespace GameFrameWork
{
    /// <summary>
    /// 游戏中的主框架，属于使用者自定义的部分
    /// </summary>
    public class GameModule : BaseModule
    {
        private Dictionary<Type,GameBaseModule> _gameModules;

        /// <summary>
        /// 注册游戏模块
        /// </summary>
        public override void Init()
        {
            RegisterGameModule<StorageModule>();
            RegisterGameModule<AssetModule>();
        }

        public override void ShutDown()
        {
            if (_gameModules == null || _gameModules.Count == 0)
                return;

            foreach(GameBaseModule module in _gameModules.Values)
            {
                module.ShutDown();
            }
        }

        public override void Update(float deltaTime, float realDeltaTime)
        {
            if (_gameModules == null || _gameModules.Count == 0)
                return;

            foreach(GameBaseModule module in _gameModules.Values)
            {
                module.Update(deltaTime, realDeltaTime);
            }
        }

        private void RegisterGameModule<T>() where T : GameBaseModule,new()
        {
            if (_gameModules == null)
                _gameModules = new Dictionary<Type,GameBaseModule>();

            Type t = typeof(T);
            if (_gameModules.ContainsKey(t))
                return;

            GameBaseModule module = new T();
            _gameModules[t] = module;
            module.Init();
        }

        public T GetModule<T>() where T : GameBaseModule
        {
            Type t = typeof(T);
            if (_gameModules.ContainsKey(t))
                return _gameModules[t] as T;

            return null;
        }
    }
}


